﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.IO;

public partial class Manager_Product_Film : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk = "";
    protected int iPageIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        /* if (Session["AdminUserID"] == null)
         {
             Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
             return;
         }*/
        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);
        if (clsAuth.IsDeleteAuth() == true)
            pDel_mk = "Y";
        if (clsAuth.IsUpdateAuth() == true)
            pUpd_mk = "Y";

        if (!IsPostBack)
        {
            txtPDID.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; ");
            txtTenSP.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; ");


            if (clsAuth.IsAddAuth() == false)
                btnAdd.Visible = false;
            BindTopicList();
            BindCatalogueByTopicID();
            if (Request.QueryString["sPdNm"] != "" && Request.QueryString["sPdNm"] != null)
                txtTenSP.Text = Request.QueryString["sPdNm"];
            if (Request.QueryString["PageIndex"] != "" && Request.QueryString["PageIndex"] != null)
            {
                iPageIndex = int.Parse(Request.QueryString["PageIndex"]);
                grv_Master.PageIndex = iPageIndex;
            }
            BinData();

        }
    }
    private void BindTopicList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlChuDe.DataSource = cls.Get_TopicNoneProc().Tables[0];
        ddlChuDe.DataTextField = "TopicName";
        ddlChuDe.DataValueField = "TopicID";
        ddlChuDe.DataBind();
        item.Text = "-- Chọn chủ đề --";
        item.Value = "0";
        ddlChuDe.Items.Insert(0, item);
        FindValueDLL(ddlChuDe, Request.QueryString["sTopicID"]);
    }

    /* private void BindStoreList()
     {
         clsWebShop cls = new clsWebShop();
         ListItem item = new ListItem();
         ddlStore.DataSource = cls.Get_StoreByKindNoNoneProc().Tables[0];
         ddlStore.DataTextField = "StName";
         ddlStore.DataValueField = "StID";
         ddlStore.DataBind();
         item.Text = "-- Chọn kho --";
         item.Value = "0";
         ddlStore.Items.Insert(0, item);
         ddlStore.SelectedItem.Selected = false;

         if (Request.QueryString["StID"] != "" && Request.QueryString["StID"] != null)
             ddlStore.Items.FindByValue(Request.QueryString["StID"]).Selected = true;
     }
     */
    private void BindCatalogueByTopicID()
    {
        clsWebShop cls = new clsWebShop();

        ListItem item = new ListItem();
        DataTable dt = null;
        dt = cls.Get_CatalogueFilmByTopicIDNoneProc(ddlChuDe.SelectedValue).Tables[0];
        ddlTheLoai.DataSource = dt;
        ddlTheLoai.DataTextField = "Catname";
        ddlTheLoai.DataValueField = "CatID";
        ddlTheLoai.DataBind();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlTheLoai.Items.Insert(0, item);
        FindValueDLL(ddlTheLoai, Request.QueryString["sCatID"]);
    }

    private void FindValueDLL(DropDownList ddl, string value)
    {
        if (value != "" && value != null)
            ddl.Items.FindByValue(value).Selected = true;
    }
    private void BinData()
    {
        PccMsg msg = new PccMsg();
        string KindNo = "";
        if (Request.QueryString["KindNo"] != "" && Request.QueryString["KindNo"] != null)
            KindNo = Request.QueryString["KindNo"];
        msg.CreateFirstNode("PdName", txtTenSP.Text.Trim());
        msg.CreateFirstNode("CatID", ddlTheLoai.SelectedValue);
        msg.CreateFirstNode("TopicID", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("StID", "0");
        msg.CreateFirstNode("KindNo", KindNo);
        msg.CreateFirstNode("PDID", txtPDID.Text.Trim());

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_FilmAdminByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddFilm.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"]);
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCatalogueByTopicID();
        BinData();
    }
    protected void Grv_Product_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells.RemoveAt(1);
        }
        else if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Cells[1].Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");

            if (DataBinder.Eval(e.Row.DataItem, "UpdID").ToString() == "-1")
            {

                Label lblUpdID = (Label)e.Row.FindControl("lblUpdID");
                lblUpdID.Text = "Administrator";
            }
            if (DataBinder.Eval(e.Row.DataItem, "AddID").ToString() == "-1")
            {

                Label lblAddID = (Label)e.Row.FindControl("lblAddID");
                lblAddID.Text = "Administrator";
            }
            //Truong hop xoa
            if (Session["IsAdmin"].ToString() != "S" && pDel_mk != "Y")
            {
                e.Row.Cells[1].Text = "";
                e.Row.Cells[1].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if (Session["IsAdmin"].ToString() != "S" && pUpd_mk != "Y")
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[0].Attributes.Add("onclick", "javascript:return false;");
            }

            clsCommon clsCom = new clsCommon();
            Label lblPrice = (Label)e.Row.FindControl("lblPrice");
            lblPrice.Text = clsCom.FormatMoney(lblPrice.Text);


            e.Row.Attributes.Add("onmouseover", "this.className='cursorOverGrid'");
            if (e.Row.RowIndex % 2 == 0)
                e.Row.Attributes.Add("onmouseout", "this.className='gvRowStyle'");
            else
                e.Row.Attributes.Add("onmouseout", "this.className='gvAlternatingRowStyle'");

            e.Row.Cells[8].Text = "<a href='ProductImages.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"] + "&sTopicID=" + ddlChuDe.SelectedValue + "&sCatID=" + ddlTheLoai.SelectedValue + "&PDID=" + DataBinder.Eval(e.Row.DataItem, "FilmID") + "'> <img id='imgpd' alt='Edit'  border='0'    src='../../Images/Icon/show_image.png' style='cursor: pointer' width='20px' height='20px' /></a>";
            e.Row.Cells[9].Text = "<a style=' text-decoration:none' href='ProductComment.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"] + "&sTopicID=" + ddlChuDe.SelectedValue + "&sCatID=" + ddlTheLoai.SelectedValue + "&PDID=" + DataBinder.Eval(e.Row.DataItem, "FilmID") + "'> <img id='imgpd' alt='Edit'   border='0'    src='../../Images/Icon/comment_stage_6.png' style='cursor: pointer;' width='20px' height='20px' /><span style='padding:5px; margin-left:2px;padding-bottom:2px;background-color:Red;color:White;'>" + DataBinder.Eval(e.Row.DataItem, "CommentCount").ToString() + "</span></a>";
        }


    }

    protected void grv_Master_RowCreated1(object sender, GridViewRowEventArgs e)
    {
        /* for (int i = 0; i < grv_Master.Rows.Count - 1; i++)
         {
             System.Web.UI.WebControls.WebControl myControl = grv_Master.Rows[i].Cells[1];
             myControl.Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
         }*/

    }

    protected void grv_Master_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("PdID", grv_Master.DataKeys[e.RowIndex].Value.ToString());
        HtmlInputHidden hImgURL = (HtmlInputHidden)grv_Master.Rows[e.RowIndex].FindControl("hImgURL");
        string Url = Server.MapPath("~/") + hImgURL.Value.Trim();
        if (File.Exists(Url))
            File.Delete(Url);

        cls.Del_Product(msg.GetXmlStr.ToString());
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BinData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void ddlTheLoai_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        iPageIndex = e.NewPageIndex;
        BinData();
    }
}
